import router from "@/router";
import store from "@/store";
import {Message} from 'element-ui'
import axios from "axios";
//本地
axios.defaults.baseURL = "http://127.0.0.1:8888/api/private/v1/"
//线上
// axios.defaults.baseURL="http://82.156.30.240:8888/api/private/v1/"

//请求前 拦截
axios.interceptors.request.use(function(config) {
    // Do something before request is sent
    config.headers.Authorization = store.getters.token
    // 发送 请求头 token
    return config;
}, function(error) {
    // Do something with request error
    return Promise.reject(error);
});

// 响应拦截
// Add a response interceptor
axios.interceptors.response.use(function(response) {
    // Do something with response data
    let success = [200, 201, 204]
    if (response.data.meta.msg == '无效token') {
        router.push('/login')
        store.commit('user/DEL_token')
        store.commit('user/DEL_username')
    } else if (success.includes(response.data.meta.status)) {
        Message.success(response.data.meta.msg)
    } else {
        Message.error(response.data.meta.msg)
    }
    return response.data;
}, function(error) {
    // Do something with response error
    return Promise.reject(error);
});


export default axios;